package com.example.zuelweb.service.impl;

import com.example.zuelweb.entity.Reresult;
import com.example.zuelweb.mappers.ImageMapper;
import com.example.zuelweb.service.FindService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class FindServiceImpl implements FindService {

    @Resource
    ImageMapper imageMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //redis查找
    @Override
    public String redisFind(String filename) {
        return stringRedisTemplate.opsForValue().get(filename);
    }

    //向redis中增添数据
    @Override
    public void redisAdd(String filename, String result) {
        stringRedisTemplate.opsForValue().set(filename,result);
    }

    //mysql查找
    @Override
    public String mysqlFind(String filename) {
        return imageMapper.findImage(filename);
    }

    //向mysql增添数据
    @Override
    public Integer mysqlAdd(String filename,String result){
        //这里返回值去掉(int)会报错，原因：返回值可能为null，而方法的返回值为int
        //Mapper method 'com.example.zuelweb.mappers.ImageMapper.add attempted to return null from a method with a primitive return type (int).
        //return (int)imageMapper.add(imageHash,result);
        //修改mapper.xml得返回值为Integer
        return imageMapper.add(filename,result);
    }

    @Override
    public List<Reresult> mysql_findall(){
        List<Reresult> list = new ArrayList<>(imageMapper.findall());
        return list;
    }

    @Override
    public void mysql_delete(String filename) {
        imageMapper.delete(filename);
        stringRedisTemplate.delete(filename);
    }


}
